//
//  ECBaseViewController.m
//  JuJuSports
//
//  Created by Jame on 15/5/14.
//  Copyright (c) 2015年 SSIC. All rights reserved.
//

#import "ECBaseViewController.h"

@interface ECBaseViewController () <UITextFieldDelegate>

@end

@implementation ECBaseViewController

#pragma mark - 加载视图
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - 创建有搜索框导航条
- (void)creatNavigationBarWithImage:(UIImage *)image
{
    //背景
    self.navigationController.navigationBarHidden =YES;
    _navcImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 64)];
    _navcImageView.backgroundColor = NAVC_COLOR;
    _navcImageView.image = image;
    _navcImageView.userInteractionEnabled = YES;
    [self.view addSubview:_navcImageView];
    
    //自定义搜索框
    _searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(ECNavcLeftButtonFrame)+3, 25, ECSCREEN_W-2*CGRectGetWidth(ECNavcLeftButtonFrame)-6, 30)];
    _searchTextField.layer.cornerRadius = 5;
    _searchTextField.layer.borderColor = [UIColor colorWithPatternImage:ECIMAGENAME(@"search_bg.png")].CGColor;
    _searchTextField.layer.borderWidth = .7f;
    _searchTextField.borderStyle = UITextBorderStyleRoundedRect;
    _searchTextField.placeholder = @"教师、课程";
    _searchTextField.delegate = self;
    _searchTextField.backgroundColor = [UIColor colorWithPatternImage:ECIMAGENAME(@"search_bg.png")];
    _searchTextField.textColor = [UIColor whiteColor];
    _searchTextField.font = [UIFont fontWithName:SystemFont size:14.f];
    _searchTextField.clearsOnBeginEditing = YES;
    _searchTextField.userInteractionEnabled = NO;
    [_navcImageView addSubview:_searchTextField];
    

    //搜索按钮图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_searchTextField.frame) - 23, 31, 20, 20)];
    imageView.image = [UIImage imageNamed:@"search_button.png"];
    imageView.userInteractionEnabled = YES;
    [_navcImageView addSubview:imageView];

    //搜索按钮
    _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_searchButton addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _searchButton.frame = CGRectMake(CGRectGetMaxX(ECNavcLeftButtonFrame)+3, 25, ECSCREEN_W-2*CGRectGetWidth(ECNavcLeftButtonFrame)-6, 30);
//    _searchButton.backgroundColor = [UIColor blueColor];
    [_navcImageView addSubview:_searchButton];
    
    self.view.backgroundColor = BG_COLOR;
}

#pragma mark - 搜索按钮触发事件
- (void)searchBtnClick:(id)sender
{
//    [_searchTextField resignFirstResponder];
    //NSLog(@"searchBtnClick");
}


#pragma mark - 自定义navigationBar&无搜索框
- (void)creatNavigationBarWithImage:(UIImage *)image title:(NSString *)title
{
    //背景
    self.navigationController.navigationBarHidden =YES;
    _navcImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 64)];
    _navcImageView.backgroundColor = NAVC_COLOR;
    _navcImageView.image = image;
    _navcImageView.userInteractionEnabled = YES;
    [self.view addSubview:_navcImageView];
    
    //标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((ECSCREEN_W - 180)/2.0f, 20, 180, 40)];
    _titleLabel.text = title;
    [_titleLabel setFont:[UIFont fontWithName:SystemFont size:18.f]];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_navcImageView addSubview:_titleLabel];
    self.view.backgroundColor = BG_COLOR;
}

#pragma mark - 创建左navigationBarItem
- (void)creatNavigationBarLeftItemWithLeftTitle:(NSString *)leftTitle LeftImage:(UIImage *)leftImage
{
    //leftLabel
    _leftLabel = [[UILabel alloc]init];
    _leftLabel.frame = ECNavcLeftLabelFrame;
    _leftLabel.text = leftTitle;
    _leftLabel.font = [UIFont fontWithName:SystemFont size:15.f];
    _leftLabel.textAlignment = NSTextAlignmentCenter;
    _leftLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_leftLabel];
    
    //leftImageView
    _leftImageView = [[UIImageView alloc]init];
    self.leftImageView.image = leftImage;
    self.leftImageView.frame = ECNavcLeftImageFrame;
    [self.view addSubview:_leftImageView];
    
    //左按钮
    _leftButton = [[UIButton alloc]init];
    [_leftButton addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _leftButton.frame = ECNavcLeftButtonFrame;
    [self.view addSubview:_leftButton];
    
}

#pragma mark - 左按钮触发事件
- (void)leftBtnClick:(id)sender
{
    //NSLog(@"leftButtonClick");
}

#pragma mark - 创建右navigationBarItem
- (void)creatNavigationBarRightItemWithRightTitle:(NSString *)rightTitle RightImage:(UIImage *)rightImage
{
    //rightLabel
    _rightLabel = [[UILabel alloc]init];
    _rightLabel.frame = ECNavcRightLabelFrame;
    _rightLabel.text = rightTitle;
    _rightLabel.font = [UIFont fontWithName:SystemFont size:15.f];
    _rightLabel.textAlignment = NSTextAlignmentCenter;
    _rightLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_rightLabel];
    
    //rightImage
    _rightImageView = [[UIImageView alloc]init];
    _rightImageView.image = rightImage;
    self.rightImageView.frame = ECNavcRightImageFrame;
    [self.view addSubview:_rightImageView];
    
    //右按钮
    _rightButton = [[UIButton alloc]init];
    [_rightButton addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _rightButton.frame = ECNavcRightButtonFrame;
    [self.view addSubview:_rightButton];
}

#pragma mark - 右按钮触发事件
- (void)rightBtnClick:(id)sender
{
    //NSLog(@"rightButtonClick");
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_searchTextField resignFirstResponder];
}

#pragma mark - 接收内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
